<template>
  <div class="employees">
    <el-card class="btn">
      <!-- <el-button
        type="primary"
        @click="goImport"
        v-if="
          $store.state.user.userInfo.roles.points.includes(
            'emplyees_import_button'
          )
        "
        >导入</el-button
      > -->
      <!-- 传统 -->
      <!-- 自定义写法 -->
      <el-button
        type="primary"
        @click="goImport"
        
        >导入</el-button
      >
      <el-button type="primary" @click="add" v-control="'emplyees_import'">+ 新增员工</el-button>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="姓名"
          prop="username"
          width="100"
        ></el-table-column>
        <el-table-column label="头像" prop="staffPhoto" width="100">
          <template v-slot="{ row }">
            <img
              class="canvas"
              :src="row.staffPhoto"
              alt=""
              @click="imgClick(row.staffPhoto)"
              v-imgerror="errorUrl"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="手机号"
          prop="mobile"
          width="150"
        ></el-table-column>
        <el-table-column
          label="工号"
          prop="workNumber"
          width="120"
        ></el-table-column>
        <el-table-column label="聘用形式" prop="formOfEmployment" width="100">
          <template v-slot="{ row }">
            <div>{{ +row.formOfEmployment | formatType }}</div>
          </template></el-table-column
        >
        <el-table-column
          label="部门"
          prop="departmentName"
          width="100"
        ></el-table-column>
        <el-table-column label="入职时间" prop="timeOfEntry" width="150">
          <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formatTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          prop="enableState"
          width="120"
        ></el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetails(row.id)">查看</el-button>
              <el-button type="text" @click="rowShow(row)">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text" @click="Run(row.id)">离职</el-button>
              <el-button type="text" @click="roleClick(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[1, 5, 10, 20]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
        >
        </el-pagination>
      </div>
    </el-card>
    <DialogRow :isShow.sync="showRow" :rowList="rowList"></DialogRow>
    <DialogItem :isShow.sync="show" @getDate="getDate"></DialogItem>
    <!-- 头像二维码 -->
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align:center">
        <canvas ref="canvas"></canvas>
      </div>
    </el-dialog>
    <!-- 分配角色 -->
    <DialogRole ref="dialogRole"></DialogRole>
  </div>
</template>

<script>
import { sysUserGet, sysUserDel, employeesPositive } from '@/api/employees'
import DialogItem from './dialogItem.vue'
import DialogRow from './components/dialogRow.vue'
import Qrcode from 'qrcode'
import DialogRole from './dialogRole.vue'
export default {
  components: {
    DialogItem,
    DialogRow,
    DialogRole
  },
  data () {
    return {
      showQrcode: false,
      errorUrl: require('@/assets/common/head.jpg'),
      show: false,
      showRow: false,
      list: [],
      rowList: [],
      page: {
        total: 100,
        page: 1,
        size: 1
      }
    }
  },
  created () {
    this.getDate()
  },
  methods: {
    async getDate () {
      const res = await sysUserGet(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log('员工列表', res)
    },
    handleCurrentChange (page) {
      this.page.page = page
      this.getDate()
    },
    handleSizeChange (size) {
      this.page.size = size
      this.page.page = 1
      this.getDate()
    },
    add () {
      this.show = true
    },
    //删除
    del (id) {
      this.$confirm('确定删除该员工吗', '提示')
        .then(async () => {
          await sysUserDel(id)
          this.$message.success('删除成功')
          this.getDate()
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    //导入页面
    goImport () {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    goDetails (id) {
      this.$router.push('/employees/details/' + id)
    },
    // updata () {
    //   this.user = JSON.parse(JSON.stringify(this.initUser))
    //   this.form.username = this.user.username
    // },
    //转正
    async rowShow (row) {
      this.showRow = true
      
      this.rowList = row
      console.log('666',row);
    
    },
    //离职
    Run (id) {
      this.$router.push('/employees/cardItem/' + id)
    },
    //显示二维码弹框
    imgClick (imgUrl) {
      if (!imgUrl) {
        this.$message.error('暂无图片')
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgUrl, {
          width: 300,
          height: 300
        })
      })
    },
    //角色
    roleClick (id) {
      this.$refs.dialogRole.isShow = true
      this.$refs.dialogRole.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 15px;
  .btn {
    text-align: right;
    margin-bottom: 15px;
  }
  .page {
    padding-top: 30px;
    text-align: right;
  }
  .canvas {
    width: 30px;
    height: 30px;
  }
}
</style>
